package com.comac.tasks.manager.app.utils;


import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.RpcUserInfoVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserEmployeeResVO;
import com.alibaba.fastjson.JSON;
import com.comac.tasks.manager.app.rpc.service.UserService;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户信息工具类
 */
@Slf4j
@Component
public class UserInfoUtil implements InitializingBean {

    @Autowired(required = false)
    private UserService userService;

    private String springProfilesActive;

    /**
     * 获取当前用户
     *
     * @return
     */
    public RpcUserInfoVO getCurrentUserInfo() {
        if("dev".equals(springProfilesActive)){
            RpcUserInfoVO userInfoVO = new RpcUserInfoVO();
            UserEmployeeResVO userEmployeeResVO = new UserEmployeeResVO();
            userEmployeeResVO.setEmployeeCode("dev");
            userEmployeeResVO.setEmployeeName("dev");
            userInfoVO.setEmployeeInfo(userEmployeeResVO);
            return new RpcUserInfoVO();
        }
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        // 判断当前用户和FO创建人是否匹配
        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        log.info("xxlSsoUser:{}", xxlSsoUser);
        RpcUserInfoVO rpcUser = null;
        if (xxlSsoUser != null) {
            log.info("当前用户登录SESSION信息：{}", JSON.toJSONString(xxlSsoUser));
            SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
            selectUserInfoReqVO.setUserId(Long.valueOf(xxlSsoUser.getUserid()));
            BaseResult<RpcUserInfoVO> rpcUserInfoVOBaseResult = userService.qryUserInfo(selectUserInfoReqVO);
            log.info("用户信息：{}", JSON.toJSONString(rpcUserInfoVOBaseResult));
            rpcUser = rpcUserInfoVOBaseResult.getData();
        }
        return rpcUser;
    }

    /**
     * 获取当前用户工号
     *
     * @return
     */
    public String getCurrentUserCode() {
        if("dev".equals(springProfilesActive)){
            return "dev";
        }
        String currentUserCode = null;
        RpcUserInfoVO currentUserInfo = this.getCurrentUserInfo();
        log.info("currentUserInfo:{}", currentUserInfo);
        if (null != currentUserInfo) {
            UserEmployeeResVO employeeInfo = currentUserInfo.getEmployeeInfo();
            if (null != employeeInfo) {
                String employeeCode = employeeInfo.getEmployeeCode();
                if (!StringUtils.isEmpty(employeeCode)) {
                    currentUserCode = employeeCode;
                }
            }
        }
        log.info("currentUserCode:{}", currentUserCode);
        return currentUserCode;
    }

    /**
     * 根据工号获取用户信息
     *
     * @return
     */
    public RpcUserInfoVO getUserInfoByCode(String code) {
        if("dev".equals(springProfilesActive)){
            return new RpcUserInfoVO();
        }
        log.info("getUserInfoByCode:{}", code);
        if (StringUtils.isBlank(code)) {
            return null;
        }
        SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
        selectUserInfoReqVO.setEmployeeCode(code);
        BaseResult<RpcUserInfoVO> rpcUserInfoVOBaseResult = userService.qryUserInfo(selectUserInfoReqVO);
        log.info("rpcUserInfoVOBaseResult:{}", rpcUserInfoVOBaseResult);
        return rpcUserInfoVOBaseResult.getData();
    }

    /**
     * 根据工号获取用户姓名
     *
     * @return
     */
    public String getUserNameByCode(String code) {
        if("dev".equals(springProfilesActive)){
            return "dev";
        }
        String userName = null;
        log.info("getUserNameByCode:{}", code);
        if (StringUtils.isBlank(code)) {
            return userName;
        }

        RpcUserInfoVO userInfo = this.getUserInfoByCode(code);
        if (null != userInfo) {
            String name = userInfo.getUserName();
            if (!StringUtils.isEmpty(name)) {
                userName = name;
            }
        }
        log.info("userName:{}", userName);
        return userName;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.springProfilesActive = System.getProperty("spring.profiles.active");
    }
}
